<?php
require 'inc_security.php';
$catBase = new Category;
$list_cat = array();
$list_cat[0] = 'Trang chủ';
$arrCat = $catBase->list_categories(0,'cat_active = 1','cat_id,cat_name,cat_type','cat_id ASC');
foreach($arrCat as $i=>$cat){
    $tt = '';
    for($j=0;$j<$cat["level"];$j++) $tt .= '|--';
    $list_cat[$cat["cat_id"]] = $tt . $cat["cat_name"];
}
$arr_cat_show = getValue('cat_list','arr','POST','');
if(!is_array($arr_cat_show)) {
    $arr_cat_show = array();
} 
$adv_category = implode(',',$arr_cat_show);

$adv_start = getValue('adv_start','str','POST','');
$adv_start = convertDateTime($adv_start);
$adv_end = getValue('adv_end','str','POST','');
$adv_end = convertDateTime($adv_end);

$myform = new generate_form();
$myform->add('adv_source','adv_source',0,0,'Tiêu đề quảng cáo');
$myform->add('adv_link','adv_link',0,0,'Link quảng cáo');
$myform->add('adv_category','adv_category',0,1,'');
$myform->add('adv_start','adv_start',1,1,0);
$myform->add('adv_end','adv_end',1,1,0,1,'Bạn chưa nhập ngày kết thúc quảng cáo');
$myform->add('adv_type','adv_type',1,0,0);
$myform->add('adv_position','adv_position',1,0,0);
$myform->add('adv_active','adv_active',1,0,0);
$myform->addTable($bg_table);
$myform->removeHTML(0);
$action = getValue('action','str','POST','');
if($action == 'execute'){
    $bg_errorMsg .= $myform->checkdata();
    $upload = new upload('adv_picture',$bg_filepath,$bg_extension,$limit_size);
    $filename = $upload->file_name;
    if($filename){
        $myform->add('adv_picture','filename',0,1,'');
        foreach($arr_resize as $type => $arr){
			resize_image($bg_filepath, $filename, $arr["width"], $arr["height"], $arr["quality"], $type);
		}
    }
    $bg_errorMsg .= $upload->show_warning_error();
    if($bg_errorMsg == ''){
        $db_insert = new db_execute($myform->generate_insert_SQL());
        //echo $myform->generate_insert_SQL();
        redirect('listing.php');
    }
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="vi" lang="vi" xmlns:og="http://ogp.me/ns#" xmlns:fb="https://www.facebook.com/2008/fbml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?=$load_header?>
</head>
<body>
<div class="module_header bold fix"><?=$module_name?></div>
<div id="wrapper">
    <?print_error_msg($bg_errorMsg)?>
	<?php $form = new form();?>
	<?=$form->form_open()?>
    <?=$form->textnote('Các trường có dấu (<span class="form-asterick">*</span>) là bắt buộc nhập')?>
	<?=$form->text(array('label'=>'Tiêu đề quảng cáo','name'=>'adv_source','id'=>'adv_source','value'=>getValue('adv_source','str','POST',''), 'placeholder'=> 'Tiêu đề không dài quá 255 ký tự'),0,'span6')?>
    <?=$form->text(array('label'=>'Link quảng cáo','name'=>'adv_link','id'=>'adv_link','value'=>getValue('adv_link','str','POST',''), 'placeholder'=> 'Link của quảng cao'),0,'span6')?>
    <div class="control-group">
        <label for="adv_category" class="control-label"><span class="form-asterick">*</span>Danh mục</label>
        <div class="controls">
    <?
        foreach($list_cat as $key=>$cat){
            ?>
            <label class="checkbox">
                <input type="checkbox" name="cat_list[]" checked="checked" value="<?=$key?>" id="cat_list_<?=$key?>">
                <?=$cat?>
            </label>
            <?
        }
        ?>
        </div>
    </div>
    <?=$form->text(array('label'=>'Ngày bắt đầu','name'=>'adv_start','id'=>'adv_start','isdatepicker'=>1,'value'=>getValue('adv_start','str','POST','')))?>
    <?=$form->text(array('label'=>'Ngày kết thúc','name'=>'adv_end','id'=>'adv_end','isdatepicker'=>1,'value'=>getValue('adv_end','str','POST','')))?>
    <?=$form->text(array('label'=>'Thứ tự - vị trí','name'=>'adv_position','id'=>'adv_position','value'=>getValue('adv_position','int','POST',0)))?>
    <?=$form->checkbox(array('label'=> 'Xuất bản', 'name'=> 'adv_active', 'id'=> 'adv_active', 'value'=>1 ,'currentValue'=>getValue('adv_active','int','POST',0), 'helptext'=> 'Xuất bản ra trang (active)'))?>
    <?=$form->getFile(array('label'=>'Ảnh quản cáo','title'=>'Ảnh quản cáo','name'=>'adv_picture','id'=>'adv_picture'))?>
    <?=$form->form_action(array('label'=>array('Thêm mới','Nhập lại'),'type'=>array('submit','reset')))?>
	<?=$form->form_close()?>
</div>
</body>
</html>